package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.bean.cp.CpBean;
import com.taptech.doufu.R;
import com.taptech.doufu.util.ImageManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/7/9.
 */
public class CpOrderListAdapter extends BaseListAdapter
{
    List<CpBean> dataList = new ArrayList<CpBean>();
    private LayoutInflater inflater; // 视图容器
    private Context context;
    private int type;
    private int selectedPosition = -1;
    private Handler handler;
    private final static int FROM_CP = 1;
    private final static int FROM_WRITE = 2;
    //private HorizontalListViewAdapter flowerUserAdapter;

    @Override
    public int getCount()
    {
        return dataList.size();
    }

    public CpOrderListAdapter(Context context, ArrayList<CpBean> dataList)
    {
        this.context = context;
        this.dataList = dataList;
        inflater = LayoutInflater.from(context);
    }


    public CpOrderListAdapter(Context context, List<CpBean> dataList, Handler handler)
    {
        this.context = context;
        this.dataList = dataList;
        this.handler = handler;
        inflater = LayoutInflater.from(context);
    }

    public CpOrderListAdapter(Context context, Handler handler)
    {
        this.type = FROM_CP;
        this.context = context;
        this.handler = handler;
        inflater = LayoutInflater.from(context);
    }

    public CpOrderListAdapter(Context context)
    {
        this.type = FROM_CP;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.item_cp_order_list, parent, false);
            holder = new ViewHolder();
            holder.cp_name = (TextView) convertView.findViewById(R.id.cp_name);
            holder.cp_iv = (ImageView) convertView.findViewById(R.id.cp_iv);
            holder.cp_rank_change_iv = (ImageView) convertView.findViewById(R.id.cp_rank_change_iv);
            holder.cp_rank_iv = (ImageView) convertView.findViewById(R.id.cp_rank_iv);
            holder.flower_num_tv = (TextView) convertView.findViewById(R.id.flower_num_tv);
            holder.cp_rank_tv = (TextView) convertView.findViewById(R.id.cp_rank_tv);
            holder.rank_change_tv = (TextView) convertView.findViewById(R.id.rank_change_tv);
            holder.cp_content_tv = (TextView) convertView.findViewById(R.id.cp_content_tv);
            holder.cp_content_change_tv = (TextView) convertView.findViewById(R.id.cp_content_change_tv);
            convertView.setTag(holder);
        } else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        CpBean data = dataList.get(position);
        holder.cp_name.setText(data.getName().trim());
        holder.cp_iv.setImageResource(R.drawable.cp_deaf_iv);
        if (data.getIcon() != null && !data.getIcon().equals(""))
        {
            ImageManager.displayImage(holder.cp_iv, data.getIcon(), 0);
        }
        holder.flower_num_tv.setText(data.getFlower_count());
        if (data.getRank() != null)
        {
            int rank_num = Integer.parseInt(data.getRank());
            if (rank_num > 0 && rank_num < 51)
            {
                holder.cp_rank_tv.setText(data.getRank());
                holder.cp_rank_iv.setVisibility(View.VISIBLE);
                holder.cp_rank_tv.setVisibility(View.VISIBLE);
            } else
            {
                holder.cp_rank_iv.setVisibility(View.GONE);
                holder.cp_rank_tv.setVisibility(View.GONE);
            }
        }
        if (data.getTrend() != null)
        {
            int trend_num = Integer.parseInt(data.getTrend());
            holder.rank_change_tv.setText(Math.abs(trend_num) + "");
            if (trend_num > 0)
            {
                holder.cp_rank_change_iv.setImageResource(R.drawable.cp_rank_up);
                holder.cp_rank_change_iv.setVisibility(View.VISIBLE);
                holder.rank_change_tv.setVisibility(View.VISIBLE);
                holder.rank_change_tv.setTextColor(Color.parseColor("#ff626c"));
            } else if (trend_num == 0)
            {
                // holder.cp_rank_iv.setImageResource(R.drawable.cp_rank_equls);
                holder.cp_rank_change_iv.setVisibility(View.GONE);
                holder.rank_change_tv.setVisibility(View.GONE);
            } else
            {
                holder.cp_rank_change_iv.setImageResource(R.drawable.cp_rank_down);
                holder.cp_rank_change_iv.setVisibility(View.VISIBLE);
                holder.rank_change_tv.setVisibility(View.VISIBLE);
                holder.rank_change_tv.setTextColor(Color.parseColor("#33cc99"));
            }
        }
        holder.cp_content_tv.setText(data.getCount());
        holder.cp_content_change_tv.setText(data.getCount());
        return convertView;
    }

    public class ViewHolder
    {
        public TextView cp_rank_tv;
        public ImageView cp_iv;
        public TextView cp_name;
        public TextView flower_num_tv;
        public ImageView cp_rank_change_iv;
        public TextView rank_change_tv;
        public TextView cp_content_tv;
        public TextView cp_content_change_tv;
        public ImageView cp_rank_iv;

    }
}
